package com.nbgroup.web;

import com.alibaba.fastjson.JSON;
import com.nbgroup.entity.Literature;
import com.nbgroup.entity.LiteratureChild;
import com.nbgroup.service.ExhibitionService;
import com.nbgroup.service.LiteratureChildService;
import com.nbgroup.service.LiteratureService;
import com.nbgroup.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 展位controller
 * 2018年8月15日, PM 02:34:36
 *
 * @author 姜文
 */
@Controller
@RequestMapping(value = "/literatureChild")
public class LiteratureChildController {

    @Autowired
    private LiteratureChildService literatureChildService;
    @Autowired
    private LiteratureService literatureService;
    @Autowired
    private ExhibitionService exhibitionService;
    @Autowired
    private FileUtil fileUtil;

    /**
     * 查询 指定展位 的子展位
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @RequestMapping("/literatureChildList")
    public String literatureChildList(Map<String, Object> map, Integer literatureId, Integer exhibtionId, Integer ichnographyId) {
        if(literatureId != null){
            map.put("literatureId", literatureId);
        }
        if(exhibtionId != null){
            map.put("exhibtionId", exhibtionId);
        }
        if(ichnographyId != null){
            map.put("ichnographyId", ichnographyId);
        }
        map.put("showUrl", fileUtil.getShowUrl());
        map.put("literatureChildList", literatureChildService.findAllForLiteratureChild(literatureId));
        map.put("literature", literatureService.findOneForLiterature(literatureId));
        return "literature/literature";
    }

    /**
     * 子展位保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/saveLiteratureChild")
    public int saveLiteratureChild(LiteratureChild literatureChild) {
        int result = literatureChildService.saveLiteratureChild(literatureChild);
        return result;
    }

    /**
     * 子展位修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/updateLiteratureChild")
    public int updateLiteratureChild(LiteratureChild literatureChild) {
        int result = literatureChildService.updateLiteratureChild(literatureChild);
        return result;
    }

    /**
     * 子展位阅读数修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/updateLiteratureChildForCount")
    public int updateLiteratureChildForCount(Integer literatureChildId) {
        int result = literatureChildService.updateLiteratureChildForCount(literatureChildId);
        return result;
    }

    /**
     * 子展位findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/findOneForLiteratureChild")
    public LiteratureChild findOneForLiterature(Integer literatureChildId){
        LiteratureChild literatureChild = literatureChildService.findOneForLiteratureChild(literatureChildId);
        return literatureChild;
    }

    /**
     * 子展位删除
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/deleteLiteratureChild")
    public int deleteLiterature(Integer literatureChildId) {
        int result = literatureChildService.deleteLiteratureChild(literatureChildId);
        return result;
    }

    /**
     * 上传图片
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping(value = "/getImgFileName", method = RequestMethod.POST)
    public String getImgFileName(@RequestParam("imgFile") MultipartFile imgFile) {
        String imgName = fileUtil.saveFile(imgFile);
        return JSON.toJSONString(imgName);
    }

    /**
     * 上传文档
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping(value = "/getPapersFileName", method = RequestMethod.POST)
    public String getPapersFileName(@RequestParam("papersFile") MultipartFile papersFile) {
        String imgName = fileUtil.saveFile(papersFile);
        return JSON.toJSONString(imgName);
    }
}
